package com.mall.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mall.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000119674226";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCYR6osG22tvyfPLO6wCvbli41Jg3g62j/liItWDwPN530zTL+gEy1lgll5tZSKlEs7E2qj0JwZq+12vqXaI906KPyjjiRkKljbN1kT30dBMuIHWMHA8lE3IOC1nD2eo5T0JjkUDh2+Z5NhK4jpizXI31kOJAai7tYTE28/2K+fcKkjptyhcX+y5DGKTKy3EfeyByWb4/Z/L3NE6ioRiBVGNPXD9ciFvqGdT4sByQXJ0RC01hyyeJOUGKWFUHX/j2tsLtFBjvD02k3WG5JRN//Mh1KiyetJVSyl+/OaJueeOeykDkdsrzh16l+KtfNiGpRBClMTgNeVOfCrghAMT19jAgMBAAECggEAH1aIN8GBRcNT/vfBOytWehDJB7rtkDb/9U2vVwuJ/eLIyjjeKqQnsH8tyLVMl2RDQyoQxRuWNFZEbS541TaSPXjX3RTQemiYlqyCQWuur0U4bMigPwPfINg8RkOcDrydV81R68Wn61pHYzThXSaP8AG+wwE4+GjsV0qll18RbV8g05WiXG2B1Wzwr+IeQg5aeTW8dwbGiyFHgJc40GmP2LbJIkBUwvX+sWCMU7MzK9K0tjAKwKkpAiGfUsypa/cpfBwI1FYnXIg2SbYPnwOCJmwHX5PivrjHe8XUowDVvC4c9sys6/P724GQ/nsLu4XBTKJ+YWbn5W+CL7ogGx6uMQKBgQDlOZ6fBGKcIS5NVdlVrUFyoCOCwYYDwEUC0ySkn8CRqEgEcFNklqga2UE1r9hWX9wDRvx9YXu0qKEN5273jWThLx6zCWMvSuJ06YePc4Z75Qwb8txoJQtQ22a5C9zqwcDGfY48se0wM+C1gBspGBiwheS2JQRU7vSnpS8KNn/OSQKBgQCqETNi1aTMPhVdbCUsLOY0j0tWOvBgr9kY7KRywT8l4d3MbSycjKfpCyyTdb9ua/Pdj3h1X5rgfV0uQ0idZHg4vzUdF6zkQPP6hbsuoqPr7Dkk0HnHC4vGyaC+GVYtvNWamhpOyMlwRA+J9lT5H77yNNITh0LNPUI8h9PQTa5wSwKBgQDOs0OnDxRAx+bx0700dhQzYdZDvXC0hzdz0aQUUv4aGo4+em+on/Ycp9YWrGe/k5dY2TUrdrFPelQoro4w6q6SJKRhO9DVv4gvdrpIEBuwMx3VQg2gV+85TW725IykoGzOUD/n2WYR9mXmJQvY5aK14zcXTI/4arXxSpH9eqGLCQKBgQCesNgzNEnHb3L+LqhtFSRmDQPuXabpgYzhbgdxMRNcpOsTExhaqAESOyQIIQpU3hS7PGHZnDFnonMtdQkwHIz13q2QDQR+JIHE1b65IHg278DOsl2/tu1hu8jrObcpjJgA4CUGOOOGh2u8f4gAwnepiFIyeLeDzdKdqtyh9ap9CwKBgEpjFiVEHHpw8RFd8c5ferSSzuxS9HzfA7WWlOXXBPp9zxeWSI1XufTmx6nFidq0Y1/COTJWnc7Mxts9j8YYMnbZKzXAfJgmH/mfpG/hjpFMiAT9qco9q89D8yhhHz2cNq1bJylVcBvObFunIgCMBegtAerXWfAiNs6CsfxL3Axh";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzJOdpCfZP31voWc59yXu0hnZbt81InrZqkGYnmWANGWN9scWpxffMPfRFbB9e7rCX9oC9xdTdOTqYrgRmdekL4VOYwiVlMaeXUHK0GH2AUrxThg+ItPgvb9040fheBmacEE1bnbP5Xg2MxGZyxmCO8sMMHKCKgk0CC7dRFXuWrbqjH9T+Ptsfw6VMdUB+ODk5ScUDW4tYOCfeIMlu4YcqSaFciKxk9OzW/2RcZM7l6X18PJgbim8z2fceghoZVrxjfa/qWY2JG4xThGeY7o09NygbQiAi0InFnbUvVocuiQ0DapW7TA+Yfv5GpGQklZ5fXGeBhqKXP41siZ5WpYS2wIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://5k21h97475.qicp.vip:43247/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.wtmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
